
//
//  BaseViewModel.m
//  OSChina
//
//  Created by 8b410-2 on 16/12/22.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCBaseViewModel.h"
#import "OSCOAuthViewModel.h"
@interface OSCBaseViewModel ()

@property (nonatomic, strong, readwrite) id<OSCViewModelServices> services;
@property (nonatomic, copy, readwrite) NSDictionary *params;

@property (nonatomic, strong, readwrite) RACSubject *errors;
@property (nonatomic, strong, readwrite) RACSubject *willDisappearSignal;

@end
@implementation OSCBaseViewModel
- (NSAttributedString *)handleHTMLSting:(NSString *)html {
    NSError *error;
    NSData *data = [html dataUsingEncoding:NSUnicodeStringEncoding];
    NSAttributedString *contentAStr = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType} documentAttributes:nil error:&error];
    if (error) {
        return [[NSAttributedString alloc] initWithString:@""];
    } else {
        return contentAStr;
    }
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    OSCBaseViewModel *viewModel = [super allocWithZone:zone];
    
    @weakify(viewModel)
    [[viewModel
      rac_signalForSelector:@selector(initWithServices:params:)]
    	subscribeNext:^(id x) {
            @strongify(viewModel)
            [viewModel initialize];
        }];
    
    return viewModel;
}

- (instancetype)initWithServices:(id<OSCViewModelServices>)services params:(NSDictionary *)params {
    self = [super init];
    if (self) {
        self.title    = params[@"title"];
        self.services = services;
        self.params   = params;
    }
    return self;
}
- (RACSubject *)errors {
    if (!_errors) {
        _errors = [RACSubject subject];
    }
    return _errors;
}
- (void)initialize {
    [self.errors subscribeNext:^(id x) {

    }];
}
- (void)presentLoginViewModel {
    OSCOAuthViewModel *loginViewModel = [[OSCOAuthViewModel alloc] initWithServices:self.services params:nil];
    [self.services presentViewModel:loginViewModel animated:YES completion:nil];
}
@end
